﻿namespace ImageProcessing
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    /// <summary>
    /// Image operations history
    /// </summary>
    public class History
    {
        /// <summary>
        /// current image open in history
        /// </summary>
        private int currentInHistory;

        /// <summary>
        /// histrory list of spicific image
        /// </summary>
        private List<DImage> historyList;

        /// <summary>
        /// Initializes a new instance of the <see cref="History"/> class.
        /// </summary>
        public History()
        {
            this.historyList = new List<DImage>();
            this.currentInHistory = 0;
        }

        /// <summary>
        /// Gets or sets the history list.
        /// </summary>
        /// <value>
        /// The history list.
        /// </value>
        public List<DImage> HistoryList
        {
            get { return this.historyList; }
            set { this.historyList = value; }
        }

        /// <summary>
        /// Gets or sets the current in history.
        /// </summary>
        /// <value>
        /// The current in history.
        /// </value>
        public int CurrentInHistory
        {
            get { return this.currentInHistory; }
            set { this.currentInHistory = value; }
        }

        /// <summary>
        /// Adds to history.
        /// </summary>
        /// <param name="newImage">The new image.</param>
        public void AddToHistory(DImage newImage)
        {
            this.historyList.Add(newImage);
            this.CurrentInHistory++;
        }

        /// <summary>
        /// Removes from history.
        /// </summary>
        /// <param name="newImage">The new image.</param>
        public void RemoveFromHistory(DImage newImage)
        {
            this.historyList.Remove(newImage);
            this.CurrentInHistory--;
        }

        /// <summary>
        /// Copies the image.
        /// </summary>
        /// <param name="image">The image.</param>
        public void CopyImage(DImage image)
        {
            this.historyList.Add(image);
            this.CurrentInHistory++;
        }

        /// <summary>
        /// Undoes this instance.
        /// </summary>
        /// <returns>Undone operation</returns>
        public DImage Undo()
        {
            if (this.currentInHistory > 0)
            {
                this.currentInHistory--;
                return this.historyList[this.CurrentInHistory];
            }
            else
            {
                return this.historyList[this.CurrentInHistory];
            }
        }

        /// <summary>
        /// Redoes this instance.
        /// </summary>
        /// <returns>Redone operation</returns>
        public DImage Redo()
        {
            if (this.currentInHistory < this.HistoryList.Count - 1)
            {
                this.currentInHistory++;
                return this.historyList[this.currentInHistory];
            }
            else
            {
                return this.historyList[this.currentInHistory];
            }
        }

        /// <summary>
        /// Gets the image in list.
        /// </summary>
        /// <param name="index">The index.</param>
        /// <returns>image found</returns>
        public ImageStructure[,] GetImageInList(int index)
        {
            return this.HistoryList[index].ImageStructArray;
        }
    }
}